package com.vae.goods.controller;

import com.vae.cart.model.Cart;
import com.vae.goods.service.SkuService;
import com.vae.mall.common.response.RespResult;
import com.vae.mall.goods.api.aggregate.AditemSku;
import com.vae.mall.goods.api.entity.Sku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Sku 控制器
 */
@CacheConfig(cacheNames = "ad-items-skus")
@RestController
@RequestMapping("/sku")
public class SkuController {

    @Autowired
    private SkuService skuService;

    /**
     *  查询指定分类下 推广产品 列表
     * @return
     */
    @Cacheable(key = "#id")
    @RequestMapping("/aditems/type")
    public RespResult<List<AditemSku>> typeitems(@RequestParam(value = "id") Integer id){
        // 查询 
        List<AditemSku> aditemSkus = skuService.typeSkuItems(id);
        return RespResult.ok(aditemSkus);
    }

    /****
     * 删除指定分类下的推广产品列表
     */
    @CacheEvict(key ="#id")
    @DeleteMapping(value = "/delitems/type")
    public RespResult deleteTypeItems(@RequestParam(value = "id") Integer id){
        //清理缓存
        skuService.delTypeSkuItems(id);
        return RespResult.ok();
    }

    /**
     * 修改指定分类下的推广产品列表
     * @param id
     * @return
     */
    @CachePut(key = "#id")
    @PutMapping(value = "/updateitems/type")
    public RespResult<List<AditemSku>> updateTypeItems(@RequestParam(value = "id") Integer id){
        //修改缓存
        List<AditemSku> aditemSkus = skuService.updateTypeItems(id);
        return RespResult.ok(aditemSkus);
    }

    /****
     * 根据skuId获取Sku
     * @param skuId
     */
    @GetMapping(value = "/{skuId}")
    public RespResult<Sku> one(@PathVariable(value = "skuId") String skuId){
        Sku sku = skuService.getById(skuId);
        return RespResult.ok(sku);
    }
    /***
     * 库存递减
     * @param carts
     * @return
     */
    @PostMapping(value = "/dcount")
    public RespResult decount(@RequestBody List<Cart> carts){
        skuService.decount(carts);
        return RespResult.ok();
    }






}